package com.superteam.zzl.view.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.superteam.zzl.activity.AuthorActivity;
import com.superteam.zzl.activity.ImageActivity;
import com.superteam.zzl.activity.InfoActivity;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.DynamicfragmentLayoutBinding;
import com.superteam.zzl.model.DynamicFragment.bean.Dynamic;
import com.superteam.zzl.view.BaseFragment;
import com.superteam.zzl.view.implement.EventMainDynamicFragment;
import com.superteam.zzl.view.implement.IMainDynamicFragmentView;
import com.superteam.zzl.viewmodel.DynamicFragment.DynamicPresenter;
import com.superteam.zzl.widget.MyImageView;


/**
 * Created by zhang on 2017/1/6.
 * 动态
 */

public class MainDynamicFragment extends BaseFragment implements IMainDynamicFragmentView, EventMainDynamicFragment {

    private DynamicfragmentLayoutBinding db;
    private DynamicPresenter dynamicPresenter;

    public static MainDynamicFragment getInstance(String type) {
        Bundle bundle = new Bundle();
        bundle.putString("type",type);
        MainDynamicFragment fragment = new MainDynamicFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dynamicPresenter = new DynamicPresenter(this, this);
        dynamicPresenter.init();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        db = DataBindingUtil.inflate(inflater,R.layout.dynamicfragment_layout,container,false);
        dynamicPresenter.Binding(db);
        dynamicPresenter.initView();
        return db.getRoot();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        dynamicPresenter.initData();
    }


    @Override
    public void show() {

    }

    @Override
    public void myClick(View view) {
         switch (view.getId()) {
            case R.id.cover_image:
                int position = ((MyImageView) view).getPosition();
                Dynamic.DataBean.TrendsListBean trendsListBean = dynamicPresenter.sendData(position);
                startActivity(new Intent(getContext(), ImageActivity.class).putExtra("data0", trendsListBean.cover_image));
                break;
            case R.id.avatar:
                int pos = ((MyImageView) view).getPosition();
                Dynamic.DataBean.TrendsListBean trends = dynamicPresenter.sendData(pos);
                startActivity(new Intent(getContext(), InfoActivity.class).putExtra("user_id", trends.author.user_id));
                break;
            default:
//        Log.e("MainDynamicFragment", "myClick: " + tag);
                int tag = ((Integer) view.getTag()).intValue();
                Dynamic.DataBean.TrendsListBean trendsListBean1 = dynamicPresenter.sendData(tag);
                startActivity(new Intent(getContext(), AuthorActivity.class).putExtra("data", trendsListBean1));
                break;
        }

    }
}
